/**
 * Created by Administrator on 2017/2/25.
 */

import {allUrl} from '../scripts/url'
import {Toast,checkLogin,getFormatDateByLong} from '../scripts/tip'

export const  inintAddress =(productType) => {

    $('input[name="productType"]').val(productType);

    if(productType==='1'){
        $('.travel').removeClass('ihide');
        $('.Material').addClass('ihide')
    }else {
        $('.travel').addClass('ihide');
        $('.Material').removeClass('ihide')
    }

    $.getJSON(''+allUrl()+'mall/queryCustomerAddress', (data) => {

        checkLogin(data, false, '');

        if(data.head.returnCode===0){
          if(data.head.subCode===0){
              var result=data.body;
              if(result){
                  var realName,telephone,province,city,dist,address,gender,idNumber,height,wechatId;
                  realName=result.realName||'';
                  telephone=result.telephone||'';

                  $('input[name="realName"]').val(realName);
                  $('input[name="telephone"]').val(telephone);

                  if(productType==='0'){

                      province=result.province||'';
                      city=result.city||'';
                      dist=result.dist||'';
                      address=result.address||'';

                     if(province!=''){
                         $("#citys").citySelect({
                             prov:province,
                             city:city,
                             dist:dist,
                             nodata:"none"
                         });
                     }
                      $('textarea[name="address"]').val(address);

                  }else {
                      gender=result.gender||'';
                      idNumber=result.idNumber||'';
                      height=result.height||'';
                      wechatId=result.wechatId||'';
                      $('input[name="gender"]').val(gender);
                      $('input[name="idNumber"]').val(idNumber);
                      $('input[name="height"]').val(height);
                      $('input[name="wechatId"]').val(wechatId);
                  }
              }
          }else {
              Toast(data.head.subMsg,2000,'');
          }
        }else {
            Toast(data.head.returnMsg,2000,'');
        }
    })

};

export const  addAddress =(productType) => {

    if(!validate(productType)) return;

    $.post(''+allUrl()+'mall/addCustomerAddress', $("#addAddress").serialize(), (data) => {

        data=$.parseJSON(data);

        checkLogin(data, false, '');

        if(data.head.returnCode===0){

            if(data.head.subCode===0){

                window.history.back();

                }else {
                        Toast(data.head.subMsg,2000,'');
                    }
            }else {
                Toast(data.head.returnMsg,2000,'');
          }
    })
};


function validate(productType){

    var realName,teletphone,province,city,dist,address,idNumber;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;

    realName= $('input[name="realName"]').val();
    teletphone=$('input[name="telephone"]').val();
    province=$('select[name="province"]').val();
    city= $('select[name="city"]').val();
    dist=$('select[name="dist"]').val();
    address=$('textarea[name="address"]').val();
    idNumber=$('input[name="idNumber"]').val();

    if(!realName||realName==='') {
        Toast('请填写姓名',2000,'');
        return false;
    }

    if(!teletphone||teletphone==='') {
        Toast('请填写手机号码',2000,'');
        return false;
    }else if(!myreg.test(teletphone)){
        Toast('请正确填写手机号码',2000,'');
        return false;
    }

    if(productType==='0'){

        if(!province||province==='') {
            Toast('请填写省份',2000,'');
            return false;
        }

        if(!city||city==='') {
            Toast('请填写城市',2000,'');
            return false;
        }

        if(!address||address==='') {
            Toast('请填写详细地址',2000,'');
            return false;
        }
    }else {
        if(!idNumber||idNumber==='') {
            Toast('请填写身份证号',2000,'');
            return false;
        }
    }

    return true;
}